home *** CD-ROM | disk | FTP | other *** search
/ Lattice ISP Synario Start… & ISP Encyclopedia 1997 / LATTICE Synario.iso / ispcode / source / isp2hex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-01  |  5.9 KB  |  146 lines

  1. /****************************************************************************
  2. *                            isp2hex.c                                      *
  3. *          ispCODE to Convert an ispSTREAM File Into ispHEX Array.          *
  4. *              Lattice Semiconductor Corp. Copyright 1996,1997              *
  5. *                                                                           *
  6. * V1.01 Howard Tang 11/19/96 Change count from unsigned int to long int.    * 
  7. * V1.02 Howard Tang 11/25/96 Correct size of buffer.                        *
  8. *                            Fix the output file name to be istream.hex     *
  9. * V1.03 Howard Tang 6/23/97  Fix the large (>64K bytes) .isp file problem.  *
  10. *****************************************************************************/
  11.  
  12. #include <stdio.h>
  13. #include <dos.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17.  
  18. short int             main(short int argc, char *argv[])
  19. {
  20.    FILE           *fpr,*fpw;
  21.    char           *ptr,
  22.                    filename[300],
  23.                    str[300];
  24.    unsigned char   data;   
  25.    /* V1.03 unsigned short*/ long int    temp,temp1;    /*V1.02 Add temp1*/
  26.    long int  count;    /*V1.01 the byte size of the ispSTREAM file*/
  27.    
  28.    printf("\n                 Lattice Semiconductor Corp.\n");
  29.    printf("\n ispSTREAM to HEX String Converter V1.03. Copyright 1996,1997.\n");
  30.  
  31.    if ((argc < 2) || (argc > 2)) {
  32.       printf("\nUsage: isp2hex [drive:][path]isp_filename\n");
  33.       printf("Example: isp2hex my_file.isp\n");
  34.       printf("\n");
  35.       printf("isp_filename         The ispSTREAM File Created By dld2isp.exe \n");
  36.       exit(1);
  37.    }
  38.    if ((fpr = fopen(argv[1], "rb")) == NULL) {
  39.       printf("\n%s File not found!\n", argv[1]);
  40.       exit(1);
  41.    }
  42.    strcpy(str, argv[1]);
  43.    ptr = strchr(str, '.');
  44.    if (ptr)
  45.       *ptr = '\0';
  46.    sprintf(filename, "%s.isp", str);
  47.    strcpy(str, argv[1]);
  48.    if (stricmp(str, filename)) {
  49.       printf("\nFile %s Is Not An ispSTREAM File\n", str);
  50.       exit(1);
  51.    }
  52.    strcpy(str,argv[1]);
  53.    ptr=strchr(str,'.');
  54.    if (ptr)
  55.     *ptr='\0';
  56. /*   sprintf(filename,"%s.hex",str);  */
  57.    sprintf(filename,"istream.hex");   /*V1.02 fix the output file name*/
  58.    if ((fpw = fopen(filename, "w")) == NULL) {
  59.       printf("\n%s File not found!\n", filename);
  60.       exit(1);
  61.    }
  62.    data = fgetc(fpr);
  63.    /*Check the ispSTREAM file type*/
  64.    count = 1;   
  65.    if (data==0x00) fprintf(fpw,"\n/*SLIM ispSTREAM File on ISP programming.*/");
  66.    else if (data==0x01)   
  67.            fprintf(fpw,"\n/*SLIM ispSTREAM File on ispJTAG programming.*/");
  68.    else if (data==0x02)   
  69.            fprintf(fpw,"\n/*Super SLIM ispSTREAM File on ISP programming.*/");
  70.    else if (data==0x03)   
  71.            fprintf(fpw,"\n/*Super SLIM ispSTREAM File on ispJTAG programming.*/");
  72.    else if (data==0x0F)   
  73.            fprintf(fpw,"\n/*ispSTREAM File on ISP programming.*/");
  74.    else if (data==0xF0)   
  75.            fprintf(fpw,"\n/*ispSTREAM File on ispJTAG programming.*/");
  76.    else if (data==0x0A)   
  77.            fprintf(fpw,"\n/*super ispSTREAM File on ISP programming.*/");
  78.    else if (data==0x0B)   
  79.            fprintf(fpw,"\n/*super ispSTREAM File on ispJTAG programming.*/");
  80.    else {printf("\nFailed: The ispSTREAM File (%s) is not Valid for ispCODE.\n",argv[1]);
  81.          fclose(fpr);
  82.          exit(1);}
  83.    /*Read the header information*/
  84.    if ((data>=0x00)&&(data<=0x04)) temp =fgetc(fpr);
  85.    else if ((data==0x0F)||(data==0xF0)) temp = fgetc(fpr) + 1;
  86.    else temp = fgetc(fpr) + 1;
  87.        /*ChainLength*/ 
  88.        count++;
  89.        fprintf(fpw,"\n/*The number of devices in the chain is %d.*/",temp); 
  90.        /*ErasePulse*/
  91.        temp = fgetc(fpr) * 0x100;              
  92.        temp += fgetc(fpr);
  93.        count += 2;
  94.        fprintf(fpw,"\n/*The bulk erase time is %d mS.*/",temp);
  95.        /*ProgramPulse*/
  96.        fprintf(fpw,"\n/*The row programming time is %d mS.*/", fgetc(fpr)); 
  97.        /*RowLength*/
  98.        temp = fgetc(fpr) * 0x100;
  99.        temp += fgetc(fpr);
  100.        count += 2;
  101.    if ((data>=0x00)&&(data<=0x04)) temp &= 0x0FFF; /*Mask out the control bits*/
  102.        fprintf(fpw,"\n/*The number of rows for programming is %d.*/",temp);
  103.        /*DataSize*/
  104.        temp1 = fgetc(fpr) * 0x100;  /*V1.02*/                
  105.        temp1 += fgetc(fpr);         /*V1.02*/
  106.        count += 2;
  107.        fprintf(fpw,"\n/*The combined length of the data registers %d.*/",temp1);
  108.        temp = fgetc(fpr) * 0x100;              
  109.        temp += fgetc(fpr);
  110.        count += 2;
  111.    if ((data>=0x00)&&(data<=0x04)) {
  112.        /*IDStreamLength*/
  113.        fprintf(fpw,"\n/*The combined length of all the ID registers %d.*/",temp);
  114.       }
  115.    else /* if ((data==0x0F)||(data==0xF0)) */ {
  116.           /*EraseRowEnd*/
  117.            fprintf(fpw,"\n/*The location where bulk erase verify end is %d.*/",temp);
  118.            temp = 0;  /*V1.02*/
  119.           }
  120.    if (temp1 >= temp) /*V1.02 allocate sufficient memory to process ID string
  121.                               or Data string*/
  122.        fprintf(fpw,"\nunsigned char buffer[%d]; /*allocate memory to process the data*/\n",temp1/8+1);
  123.    else
  124.       fprintf(fpw,"\nunsigned char buffer[%d]; /*allocate memory to process the data*/\n",temp/8+1);
  125.     
  126.    fprintf(fpw,"\n");       
  127.    while (!feof(fpr))
  128.         {fgetc(fpr);
  129.          count++;   /*count all the bytes till the end*/
  130.         }
  131.    fclose(fpr);
  132.    /*restore the pointer to the beginning of the file*/
  133.    fpr = fopen(argv[1],"rb");
  134.    fprintf(fpw,"unsigned char ispstream[%ld] = {\n",count);   /*v1.01*/
  135.    for (temp=1; temp<count; temp++)
  136.        { fprintf(fpw,"0x%02X,",fgetc(fpr));
  137.          if (temp%15==0) fprintf(fpw,"\n");    /*make the file more readable*/
  138.        }
  139.    fprintf(fpw,"0x%02X",fgetc(fpr));
  140.    fprintf(fpw,"};\n");
  141.    fclose(fpw);
  142.    fclose(fpr);
  143.    printf("\nispHEX File (%s) Generated.\n",filename); 
  144.    return(0);
  145.